package dhm.com.dhmshop.view.mine;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dhm.com.dhmshop.R;
import dhm.com.dhmshop.adapter.mine.EvaluateAdapter;
import dhm.com.dhmshop.adapter.mine.TopingAdapter;
import dhm.com.dhmshop.base.BaseActiity;
import dhm.com.dhmshop.base.Presenter.PressenterImpl;
import dhm.com.dhmshop.base.netWork.Constant;
import dhm.com.dhmshop.base.netWork.LoginContract;
import dhm.com.dhmshop.entity.GetUserInfo;
import dhm.com.dhmshop.entity.Myorder;
import dhm.com.dhmshop.utils.SpUtils;

public class EvaluateActivity extends BaseActiity implements LoginContract.IView {

    @BindView(R.id.image)
    SimpleDraweeView image;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.alleval)
    TextView alleval;
    @BindView(R.id.num)
    TextView num;
    @BindView(R.id.recy)
    RecyclerView recy;
    @BindView(R.id.allrecy)
    RecyclerView allrecy;
    private PressenterImpl pressenter;
    private String uid;

    private String is_evaluate = "0";
    private TopingAdapter topingAdapter;
    private EvaluateAdapter evaluateAdapter;
    private int num1;

    @Override
    protected int getLayout() {
        return R.layout.activity_evaluate;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        pressenter = new PressenterImpl();
        pressenter.attachView(this);
        uid = SpUtils.getString(this, "uid");
        Map<String, String> map = new HashMap<>();
        map.put("token", Constant.TOKEN);
        map.put("uid", uid);
        pressenter.sendMessage(EvaluateActivity.this, Constant.AllordersToEvaluate, map, Myorder.class);
    }

    @Override
    protected void initData() {
        LinearLayoutManager evaManager=new LinearLayoutManager(this);
        evaManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        LinearLayoutManager allevaManager=new LinearLayoutManager(this);
        allevaManager.setOrientation(LinearLayoutManager.VERTICAL);
        topingAdapter = new TopingAdapter(this);
        evaluateAdapter = new EvaluateAdapter(this);
        recy.setLayoutManager(evaManager);
        recy.setAdapter(topingAdapter);
        allrecy.setLayoutManager(allevaManager);
        allrecy.setAdapter(evaluateAdapter);
        getuserinfo();
    }


    @Override
    public void requesta(Object data) {
        if (data instanceof Myorder) {
            Myorder myorder = (Myorder) data;
            if (myorder.getCode() == 1) {
                if (is_evaluate.equals("0")) {
                    is_evaluate = "1";
                    num1 = 0;
                    topingAdapter.setShopList(myorder.getData());
                    Map<String, String> map = new HashMap<>();
                    map.put("token", Constant.TOKEN);
                    map.put("uid", uid);
                    map.put("is_evaluate", "1");
                    pressenter.sendMessage(this, Constant.Allorders, map, Myorder.class);
                    num1=myorder.getData().size();
                    num.setText(num1+"");
                }else {
                    num1=myorder.getData().size()+num1;
                    evaluateAdapter.setShopList(myorder.getData());
                    getuserinfo();
                }
            }
        }else if (data instanceof GetUserInfo){
            GetUserInfo getUserInfo= (GetUserInfo) data;
            image.setImageURI(Constant.PATH+"/"+getUserInfo.getData().getHeadsmall());
            name.setText(getUserInfo.getData().getUser_nickname());
            alleval.setText("全部评价:"+num1);
        }
    }
    protected void getuserinfo() {
        Map<String,String> map=new HashMap<>();
        map.put("token",Constant.TOKEN);
        map.put("uid",uid);
        pressenter.sendMessage(EvaluateActivity.this, Constant.GetUserInfo,map, GetUserInfo.class);

    }

    @Override
    public void fail(String error) {

    }

    @OnClick({R.id.back, R.id.seeall})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.seeall:
                Intent intent=new Intent(EvaluateActivity.this, MyorderActivity.class);
                intent.putExtra("type","4");
                startActivity(intent);
                break;
                default:
        }
    }
}
